/**   
 * @Title: SystemProperties.java 
 * CopyRight(c)2010-DMX 
 */
package com.dmx.fw.resource;

import java.lang.reflect.InvocationTargetException;
import java.util.Properties;

import org.apache.log4j.Logger;

import com.dmx.fw.util.StrUtils;

/** 
 * @Description: 系统参数
 * @author: system
 * @date: 2008
 * @version V1.0 
 * 
 */
public class SystemProperties {
	
	/** 
	 * @Fields Current_DataBase: 系统当前使用的数据库
	 */
	private final static String Current_DataBase = "framework.base.currentDB";
	
	
	/** 
	 * @Fields PAGE_SIZE: 页面行数
	 */
	private final static String PAGE_SIZE = "framework.base.pageSize";
	
	private final static String INITIALCONTEXTFACTORY = "framework.base.initialContextFactory";
	
	private final static String PROVIDERURL = "framework.base.providerUrl";
	
	private final static String SECURITY_PRINCIPAL = "framework.base.SecurityPrincipal";
	
	private final static String SECURITY_CREDENTIALS = "framework.base.SecurityCredentials";
	
	/** 
	 * @Fields DATASOURCE: 数据源的jndi名称
	 */
	private final static String DATASOURCE = "framework.base.datasource";
	
	private final static String DEFAULTPASSWORD="framework.base.defaultpassword";//默认密码
	
	private final static String DEFAULTDATE="framework.base.defaultdate";//默认日期
	
	
	/** 
	 * @Fields EXPORT_PAGE_SIZE: 使用控件进行数据导出时，每页查询的行数，默认是5000
	 */
	private final static String EXPORT_PAGE_SIZE="framework.base.exportPageSize";
	
	
	
	private static Properties properties = null;
	private final static Logger log =Logger.getLogger(SystemProperties.class);
	
	private static SystemProperties instance = new SystemProperties();
	private SystemProperties(){		
	}
	
	public static SystemProperties getInstance(){		
		return instance;
	}
	
	public String getPropertyVal(String propertyName){
		if (properties==null){
			synchronized(SystemProperties.class){
				try{
					properties = new Properties();
					properties.load(this.getClass().getResourceAsStream("/system.properties"));
				}catch(Exception e){
					log.error(e);
					properties = null;
				}
			}
		}
		
		if (properties!=null){
			return properties.getProperty(propertyName);
		}
		
		return null;
		
	}
	
	/**
	 * @Description return Current_DataBase 返回当前使用数据库类型参数
	 * @return String
	 */
	public String getCurrentDataBase(){
		String retDB = getPropertyVal(Current_DataBase);
		if(retDB == null){
			retDB="oracle"; //默认为oracle数据库
		}
		return retDB;
	}
	
	/**
	 * @Description return PAGE_SIZE
	 * @return int
	 */
	public int getPageSize(){
		String strPageSize = getPropertyVal(PAGE_SIZE);
		
		return StrUtils.toInteger(strPageSize,15);
	}
	
	public String getInitialContextFactory(){
		return getPropertyVal(INITIALCONTEXTFACTORY);
	}
	
	public String getProviderUrl(){
		return getPropertyVal(PROVIDERURL);
	}
	
	public String getSecurityPrincipal(){
		return getPropertyVal(SECURITY_PRINCIPAL);
	}
	
	public String getSecurityCredentials(){
		return getPropertyVal(SECURITY_CREDENTIALS);
	}
	
	public String getDataSourceJndiName(){
		return getPropertyVal(DATASOURCE);
	}
	
	public String getDefaultPassword(){
		return getPropertyVal(DEFAULTPASSWORD);
	}
	
	public String getDefaultDate(){
		return getPropertyVal(DEFAULTDATE);
	}
	
	public int getExportPageSize(){
		String exportPageSize = getPropertyVal(EXPORT_PAGE_SIZE);
		int pageSize = 5000;
			if (exportPageSize!=null){
			try{			
				pageSize = Integer.parseInt(exportPageSize);					
			}catch(Exception e){
				pageSize = 5000;
			}
		}
		return pageSize;
		
	}
	

}
